package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Goods;
import cn.wolfcode.car.business.domain.GoodsCategory;
import cn.wolfcode.car.business.mapper.GoodsCategoryMapper;
import cn.wolfcode.car.business.mapper.GoodsMapper;
import cn.wolfcode.car.business.query.GoodsCategoryQuery;
import cn.wolfcode.car.business.service.IGoodsCategoryService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class GoodsCategoryServiceImpl implements IGoodsCategoryService {
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public TablePageInfo<GoodsCategory> query(GoodsCategoryQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<GoodsCategory>(goodsCategoryMapper.selectForList(qo));
    }

    @Override
    public void save(Long superId,GoodsCategory goodsCategory) {
        //将数据保存到数据库
        int insert = goodsCategoryMapper.insert(goodsCategory);
        //如果父类id等于null
        if (superId == null){
            //如果父类id为null 使用自己的id 作为自己的BusiPath
            goodsCategory.setBusiPath(goodsCategory.getId().toString());
        } else {
            //如果父类id不为null 查询出父类的BusiPath 拼接自己的id 作为自己的BusiPath
            GoodsCategory superCategory = goodsCategoryMapper.selectByPrimaryKey(superId);
            goodsCategory.setBusiPath(superCategory.getBusiPath() + "/" + goodsCategory.getId());
            goodsCategory.setParentId(superId);
        }
        //修改BusiPath数据
        goodsCategoryMapper.updateByPrimaryKey(goodsCategory);
    }

    @Override
    public GoodsCategory get(Long id) {
        return goodsCategoryMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(GoodsCategory goodsCategory) {
        goodsCategoryMapper.update(goodsCategory);
    }

    @Override
    public List<GoodsCategory> list() {
        return goodsCategoryMapper.selectAll();
    }


    @Override
    @Transactional
    public void deleteCategory(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);

        for (Long dictId : dictIds) {

            //根据 id 查询出该分类下 所有子类(包含本生)集合
            List<GoodsCategory> list = goodsCategoryMapper.selectByparentId(dictId);

            //判断该分类及子类有没有库存
            //遍历得出所有子类对象
            for (GoodsCategory goodsCategory : list) {
                //根据子类id 查询商品表,获取所有属于该类的商品()
                List<Goods> goods = goodsCategoryMapper.selectByCategoryId(goodsCategory.getId());
                for (Goods good : goods) {
                    //根据商品id 查询中间表数据 ,获取商品库存数量
                    List<Integer> goodsCounts = goodsCategoryMapper.goodsCount(good.getId());
                    for (Integer goodsCount : goodsCounts) {
                        if (goodsCount != 0){
                            throw new BusinessException("不能删除有库存商品的分类");
                        }
                    }
                }
            }
            //删除本类 和 所有子类
            for (GoodsCategory goodsCategory : list) {
                goodsCategoryMapper.deleteByPrimaryKey(goodsCategory.getId());
            }
        }
    }
}
